package com.nl.search;

public class FindSumEqualN {
	
	//排序数组中的两个数和为Ｎ
	public static void findTwoNumberSumEQN(int[] array,int n){
		if(array==null ||array.length<2){
			return ;
		}
		
		int first=0,last = array.length-1;
		boolean found=false;
		while(first<last){
			int sum = array[first] + array[last];
			if(sum>n){
				last--;
			}else if( sum <n){
				first++;
			}else{
				found = true;
				break;
			}
		}
		
		if(found){
			System.out.println(array[first]+" + "+array[last] +" = "+n);
		}else{
			System.out.println("Not found");
		}

	}
	
	
	//和为连续的正整数和的序列
	public static void  findSeqs(int sum){
		int first = 1,last=2;
		int mid = (1+sum)/2;
		int currentSum = first+last;
		while(first<mid){
			if(currentSum == sum){
				printSeq(first,last);
			}
			while(first<mid && currentSum>sum){
				currentSum -=first;
				first++;
				if(currentSum == sum){
					printSeq(first, last);
				}
			}

			last++;
			currentSum+=last;
		}
	}
	private static void printSeq(int first, int last) {
		for(int i=first;i<=last;i++){
			System.out.print(i+" ");
		}
		System.out.println();
	}


	public static void main(String[] args) {
		int[]array = new int[]{1,2,3,44,55,66,230,1000};
		findTwoNumberSumEQN(array, 232);
		findSeqs(15);
	}

}
